package com.ruoyi.project.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.wx.domain.TreatyDO;
import com.ruoyi.project.wx.service.TreatyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户协议及隐私政策Controller
 *
 * @author ruoyi
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/wx/wxCmsTreaty")
class CmsTreatyController extends BaseController
{
	@Autowired
	private TreatyService cmsTreatyService;

	/**
	 * 查询用户协议及隐私政策列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxCmsTreaty:list')")
	@GetMapping("/list")
	public TableDataInfo list(TreatyDO cmsTreaty)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(cmsTreaty);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		List<TreatyDO> list = cmsTreatyService.list(stringObjectMap);
		return getDataTable(list);
	}

	/**
	 * 导出用户协议及隐私政策列表
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsTreaty:export')")
	@Log(title = "用户协议及隐私政策", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(TreatyDO cmsTreaty)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(cmsTreaty);
		List<TreatyDO> list = cmsTreatyService.list(stringObjectMap);
		ExcelUtil<TreatyDO> util = new ExcelUtil<TreatyDO>(TreatyDO.class);
		return util.exportExcel(list, "wxCmsTreaty");
	}

	/**
	 * 获取用户协议及隐私政策详细信息
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsTreaty:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Integer id)
	{
		return AjaxResult.success(cmsTreatyService.get(id));
	}

	/**
	 * 新增用户协议及隐私政策
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsTreaty:add')")
	@Log(title = "用户协议及隐私政策", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody TreatyDO cmsTreaty)
	{

		cmsTreaty.setDelFlag(0);
		cmsTreaty.setCaAt(new Date());
		return toAjax(cmsTreatyService.save(cmsTreaty));
	}

	/**
	 * 修改用户协议及隐私政策
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsTreaty:edit')")
	@Log(title = "用户协议及隐私政策", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody TreatyDO cmsTreaty)
	{
		cmsTreaty.setUpAt(new Date());
		return toAjax(cmsTreatyService.update(cmsTreaty));
	}

	/**
	 * 删除用户协议及隐私政策
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsTreaty:remove')")
	@Log(title = "用户协议及隐私政策", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Integer[] ids)
	{
		if(ids.length>0){
			for (Integer string : ids) {
				TreatyDO cmsTreaty = cmsTreatyService.get(string);
				cmsTreaty.setDelFlag(1);
				cmsTreaty.setUpAt(new Date());
				cmsTreatyService.update(cmsTreaty);
			}
		}
		return toAjax(ids.length);
		//return toAjax(cmsTreatyService.deleteCmsTreatyByIds(ids));
	}
}
